import json

def extract_json_from_response(response):
    """从LLM响应中提取JSON结构"""
    try:
        start_idx = response.find('{')
        end_idx = response.rfind('}') + 1
        if start_idx == -1 or end_idx == -1:
            return None
        json_str = response[start_idx:end_idx]
        return json.loads(json_str)
    except:
        return None

def split_response(response):
    """
    分离LLM响应中的对话文本和JSON部分
    返回: (对话文本, JSON字典) 或 (原始文本, None)
    """
    try:
        json_start = response.find('{')
        json_end = response.rfind('}') + 1
        
        if json_start == -1 or json_end == 0:
            return response, None
            
        dialog_text = response[:json_start].strip()
        json_part = response[json_start:json_end]
        
        return dialog_text, json.loads(json_part)
    except:
        return response, None